package com.zx.sp_mybatisplus_smbms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zx.sp_mybatisplus_smbms.pojo.SmbmsProvider;
import com.zx.sp_mybatisplus_smbms.service.SmbmsProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/provider/")
public class ProviderController {
    @Autowired
    private SmbmsProviderService providerService;
    @RequestMapping("list/{current}")
    public String list(@PathVariable("current") Integer current,Model model){
        if (current==null){
            current=1;
        }


        Page<SmbmsProvider> page=new Page<SmbmsProvider>(current,5);
        providerService.page(page);
        model.addAttribute("ppage",page);
        return "providerlist";
    }

    //2条件查询所有
    @RequestMapping("find")
    public String find(Integer current,String proname,Long createdby, Model model) {
        if(current==null){
            current=1;
            System.out.println(current);
        }

        Integer total;//总页数
        List<SmbmsProvider> list=providerService.list();
        total=list.size()/5;
        if (list.size()%5!=0){
            total=list.size()/5+1;
        }else {
            total=list.size()/5;
        }

        //如果跳转的页面大于总页数就直接到最后一页
        if (current>total){
            current=total;
            System.out.println(current);
        }
        System.out.println(current);
        Page<SmbmsProvider> page=new Page<SmbmsProvider>(current,5);
        QueryWrapper<SmbmsProvider> wrapper =new QueryWrapper<>();
        if(proname!=null&&proname!="null"){
            wrapper.like("proname",proname);
        }
        if(createdby!=null&&!"null".equals(createdby)){
            wrapper.eq("createdby",createdby);
        }
        for (SmbmsProvider s:page.getRecords()) {
            System.out.println(s);
        }
        providerService.page(page,wrapper);
        model.addAttribute("ppage", page);
        model.addAttribute("current",current);
        model.addAttribute("proname",proname);
        model.addAttribute("createdby",createdby);

        return "providerlist";
    }

    //详情页面
    @RequestMapping("findById")
    public String findById(Long uid, Model model) {
        SmbmsProvider provider = providerService.findProviderById(uid);
        model.addAttribute("provider", provider);
        return "providerview";
    }

    //删除操作
    @RequestMapping("del")
    public String del(Long uid, Model model) {
        boolean flag = providerService.removeById(uid);
        if (flag) {
            return "redirect:list/1";
        } else {
            model.addAttribute("msg", " 删除失败");
            return "providerlist";
        }
    }

    //修改前查询
    @RequestMapping("goupdate")
    public String goupdate(Long uid, Model model) {
        SmbmsProvider provider = providerService.findProviderById(uid);
        model.addAttribute("provider", provider);
        return "providermodify";
    }

    //修改操作
    @RequestMapping("doupdate")
    public String doupdate(SmbmsProvider provider, Model model) {
        boolean flag = providerService.updateById(provider);
        if (flag) { //修改成功  再次查詢下
            return "redirect:list/1";
//            return "forward:list";
        } else {  //修改失败
            model.addAttribute("msg", "修改失败！");
            return "providermodify";
        }
    }

    //添加页面跳转
    @RequestMapping("goAdd")
    public String goAdd() {
        return "provideradd";
    }

    //添加操作
    @RequestMapping("add")
    public String add(SmbmsProvider provider, Model model) {
        System.out.println(provider);
        boolean flag = providerService.save(provider);
        if (flag) {
            return "redirect:list/1";
        } else {
            model.addAttribute("msg", " 添加失败");
            return "provideradd";
        }
    }

}
